package lg.iu;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;

public class ReadURLVersion {

    

    public static void fetchNewest(final File rsjar) throws IOException {
        final URL jarurl = new URL("http://world2.runescape.com/runescape.jar");
        final URLConnection conn = jarurl.openConnection();
        conn.setRequestProperty(
                "User-Agent",
                "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");

        if (rsjar.exists()) {
            rsjar.delete();
        }

        final ReadableByteChannel rbc = Channels.newChannel(conn
                .getInputStream());
        final FileOutputStream fos = new FileOutputStream(rsjar);
        fos.getChannel().transferFrom(rbc, 0, 1 << 24);

        /*
         * final JarFile jf = new JarFile(cached);
         * 
         * final Enumeration<JarEntry> entries = jf.entries(); while
         * (entries.hasMoreElements()) { final JarEntry je =
         * entries.nextElement();
         * 
         * 
         * if (je.getName().contains("META-INF")) { continue; }
         * 
         * final File writeTo = new File(unpackedLatestArchive, je.getName());
         * 
         * if (!writeTo.getParentFile().exists()) { if
         * (!writeTo.getParentFile().getParentFile().exists()) {
         * writeTo.getParentFile().getParentFile().mkdir(); // <-- stupid }
         * writeTo.getParentFile().mkdir(); }
         * 
         * final InputStream in = jf.getInputStream(je); final OutputStream out
         * = new FileOutputStream(writeTo);
         * 
         * // Transfer bytes from in to out final byte[] buf = new byte[1024];
         * int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); }
         * 
         * in.close(); out.close();
         * 
         * }
         * 
         * jf.close();
         */

    }
}
